﻿using AutoMapper.Internal.Mappers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Himp.Platform
{
    /// <summary>
    /// 标准用语
    /// </summary>
    public class StdPhrDictService : CrudAppService<StdPhrDict, StdPhrDictDto, Guid, StdPhrDictInput, StdPhrDictCreateDto, StdPhrDictUpdateDto>
    {
        private IRepository<StdPhrDict, Guid> _StdPhrDictRepository;

        public StdPhrDictService(IRepository<StdPhrDict, Guid> StdPhrDictRepository) : base(StdPhrDictRepository)
        {
            _StdPhrDictRepository = StdPhrDictRepository;
        }

        /// <summary>
        /// 获取标准用语
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public override async Task<PagedResultDto<StdPhrDictDto>> GetListAsync(StdPhrDictInput input)
        {
            var iQuery = await _StdPhrDictRepository.WithDetailsAsync();
            iQuery = iQuery.WhereIf(!string.IsNullOrEmpty(input.Filter), i => i.PhrCode.Contains(input.Filter) || i.PhrName.Contains(input.Filter));
            iQuery = iQuery.WhereIf(!string.IsNullOrEmpty(input.PhrCode), i => i.PhrCode == input.PhrCode);


            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<StdPhrDict>, List<StdPhrDictDto>>(iQuery.ToList());

                return new PagedResultDto<StdPhrDictDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<StdPhrDict>, List<StdPhrDictDto>>(items.ToList());

                return new PagedResultDto<StdPhrDictDto>(totalCount, itemsDto);
            }
        }

        /// <summary>
        /// 批量上传
        /// </summary>
        /// <returns></returns>
        public async Task PostBatchAsync(List<StdPhrDictCreateDto> input)
        {
            var StdPhrDicts = ObjectMapper.Map<List<StdPhrDictCreateDto>, List<StdPhrDict>>(input);
            await _StdPhrDictRepository.InsertManyAsync(StdPhrDicts);
        }
    }
}
